package com.zhizuobiao.coura.stockhelper;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.zhizuobiao.coura.adapter.GuidePageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by coura on 2017/1/3.
 */

public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.guide_viewpager)
    ViewPager guideViewpager;
    @BindView(R.id.guide_viewgroup)
    LinearLayout guideViewgroup;
    private int[] ids = {R.mipmap.guide_pic1, R.mipmap.guide_pic2, R.mipmap.guide_pic3, R.mipmap.guide_pic4};
    private GuidePageAdapter guidePageAdapter;
    private List<ImageView> list;
    private int lastPosition;
    private static final String SHAREDPREFERENCES_NAME = "my_pref";
    private static final String KEY_GUIDE_ACTIVITY = "guide_activity";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        list = new ArrayList<ImageView>();
        for (int i = 0; i < ids.length; i++) {
            ImageView image = new ImageView(this);
            image.setBackgroundResource(ids[i]);
            list.add(image);
            ImageView point = new ImageView(this);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.rightMargin = 20;
            point.setLayoutParams(params);
            point.setBackgroundResource(R.drawable.selector_point);
            point.setEnabled(false);
            if (i == 0) {
                point.setEnabled(true);
            }
            guideViewgroup.addView(point);
        }
        guidePageAdapter = new GuidePageAdapter(list);
        guideViewpager.setAdapter(guidePageAdapter);
        guideViewpager.addOnPageChangeListener(this);
        guideViewpager.setEnabled(false);
        list.get(list.size()-1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置已经引导
                setGuideed();
                //跳转
                Intent intent = new Intent(GuideActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        guideViewgroup.getChildAt(position).setEnabled(true);
        guideViewgroup.getChildAt(lastPosition).setEnabled(false);
        lastPosition = position;
        if (position == (list.size() - 1)) {
            guideViewpager.setEnabled(true);
        } else {
            guideViewpager.setEnabled(false);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void setGuideed() {
        SharedPreferences settings = getSharedPreferences(SHAREDPREFERENCES_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(KEY_GUIDE_ACTIVITY, "false");
        editor.commit();
    }
}
